package org.example.petitionplatformsystem.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.example.petitionplatformsystem.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(Map<String, Object> param, String phone) throws ClientException {
        if (Objects.isNull(phone) || phone.isEmpty()) return false;

        //default 地域节点，默认  后面是 阿里云的 id和秘钥
        DefaultProfile profile = DefaultProfile.getProfile("default",
                "LTAI5tGpvFnKBemuS1TrhvQv",
                "4EbYQPqmpLEBF7eORx4efDOivADLcB");
        IAcsClient client = new DefaultAcsClient(profile);

        // 创建 SendSmsRequest 对象
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("智慧信访平台"); // 短信签名
        request.setTemplateCode("SMS_469060120"); // 短信模板
        request.setPhoneNumbers(phone); // 手机号码
        request.setTemplateParam(JSONObject.toJSONString(param)); // 模板参数

        // 发送短信
        SendSmsResponse response = client.getAcsResponse(request);
        System.out.println(response.getMessage());

        return true;
    }
}
